<?php
set_time_limit(0);

include_once('lib/config.php');

$redis = new Redis();
$redis->connect(REDIS_IP,"6379");  //php客户端设置的ip及端口
if(!$redis) die('redis connect fail');

$limit = 100;
while($limit--) {
  $stop = $redis->get('LNPAN_QUEUE_STOP');
  if($stop) {
    break;
  }

  $url = $redis->lPop(KEY_QUEUE_TO_DOWN);
  if ($url) {
    $localDir = dirname(__FILE__).'/Public';
    $cmd  = 'cd '.$localDir.';';
    $cmd .= 'wget --limit-rate=1000k --continue --timeout=120 --tries=5 --no-clobber --debug --force-directories --no-host-directories '.$url;
    $result = shell_exec($cmd);

    $parse = parse_url($url);
    $filepath = $localDir.$parse['path'];
    clearstatcache();
    $filesize = (int) filesize($filepath);

    if(!file_exists($filepath) || $filesize < 5000) {
      $redis->rPush(KEY_QUEUE_TO_DOWN, $url);
    } else {
      $redis->lPush(KEY_QUEUE_LOCAL_DOWN_SUCCESS, date('Y-m-d H:i:s').':http://23.88.238.143/lnpan/Public'.$parse['path']);
      $redis->rPush(KEY_QUEUE_TO_UPLOAD, $url);
    }
    //echo date('Y-m-d H:i:s'). ' # '.$url.' # '.$filepath;
    //echo "\n";
  } else {
    sleep(1);
  }

  //break;
}
